package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKsxsyBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKsxsyBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKsxsyBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKsxsyBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKsxsyBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土抗渗性试验检测试件记录详情Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKsxsyBaDetailServiceImpl implements IReportSnhntKsxsyBaDetailService {

    private final ReportSnhntKsxsyBaDetailMapper baseMapper;

    /**
     * 查询水泥混凝土抗渗性试验检测试件记录详情
     */
    @Override
    public ReportSnhntKsxsyBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水泥混凝土抗渗性试验检测试件记录详情列表
     */
    @Override
    public TableDataInfo<ReportSnhntKsxsyBaDetailVo> queryPageList(ReportSnhntKsxsyBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKsxsyBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKsxsyBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土抗渗性试验检测试件记录详情列表
     */
    @Override
    public List<ReportSnhntKsxsyBaDetailVo> queryList(ReportSnhntKsxsyBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntKsxsyBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKsxsyBaDetail> buildQueryWrapper(ReportSnhntKsxsyBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKsxsyBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntKsxsyBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getKsxsysjId() != null, ReportSnhntKsxsyBaDetail::getKsxsysjId, bo.getKsxsysjId());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), ReportSnhntKsxsyBaDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getRiq() != null, ReportSnhntKsxsyBaDetail::getRiq, bo.getRiq());
        lqw.eq(bo.getShij() != null, ReportSnhntKsxsyBaDetail::getShij, bo.getShij());
        lqw.eq(StringUtils.isNotBlank(bo.getShuiy()), ReportSnhntKsxsyBaDetail::getShuiy, bo.getShuiy());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyqk()), ReportSnhntKsxsyBaDetail::getShiyqk, bo.getShiyqk());
        return lqw;
    }

    /**
     * 新增水泥混凝土抗渗性试验检测试件记录详情
     */
    @Override
    public Boolean insertByBo(ReportSnhntKsxsyBaDetailBo bo) {
        ReportSnhntKsxsyBaDetail add = BeanUtil.toBean(bo, ReportSnhntKsxsyBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水泥混凝土抗渗性试验检测试件记录详情
     */
    @Override
    public Boolean updateByBo(ReportSnhntKsxsyBaDetailBo bo) {
        ReportSnhntKsxsyBaDetail update = BeanUtil.toBean(bo, ReportSnhntKsxsyBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKsxsyBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土抗渗性试验检测试件记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
